'use strict';

const Service = require('egg').Service;

class UtilsService extends Service {
  async getAccessToken() {
    const { ctx, app } = this
    const wxApp = app.config.appConfig['wx']
    let url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${wxApp.appid}&secret=${wxApp.secret}`
    let token = await ctx.curl(url, { dataType: 'json' })
    if (token.status == 200) {
      return token.data.access_token
    } else {
      ctx.throw(400,'获取access_token失败!')
    }
  }
}

module.exports = UtilsService;
